package com.nondo.dean.saprfc.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.saprfc.common.Util;
import com.nondo.dean.saprfc.domain.Customer;
import com.nondo.dean.saprfc.domain.Region;
import com.nondo.dean.saprfc.service.CrmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author xiaofan
 * @Description:
 * @throws
 * @date 2018-04-1211:13
 * @Version
 */
@RestController
@RequestMapping("/{v}/saprfc")
public class CrmUserController {

    @Autowired
    CrmUserService crmUserService;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 获取sap的经销商信息
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/user/list")
    @HystrixCommand(fallbackMethod = "ErrorFallback")
    public View<String> getUser(){
        View<String> view = new View<>(20000,"同步成功");
        Util.reInitMessageConverter(restTemplate);
        List<Region> regionList=crmUserService.getSapRegion("");
        String resultRegion= JSON.toJSONString(regionList);
        //地区
        Integer regionMessage= restTemplate.postForEntity("http://dean-platform/platform/v1/inner/user/region", resultRegion, Integer.class).getBody();

        List<Customer> customerList = crmUserService.getSapCustomer("");
        String resultCustomer = JSON.toJSONString(customerList);
        //经销商
        Integer customerMessage= restTemplate.postForEntity("http://dean-platform/platform/v1/inner/user/list", resultCustomer, Integer.class).getBody();
        //权限经销商
        Integer authUserMessage= restTemplate.postForEntity("http://dean-auth/auth/v1/inner/auth/user/list", resultCustomer, Integer.class).getBody();

       view.setData("更新了地区'"+(regionMessage)+"'条数据,更新了经销商'"+customerMessage+"'条数据,更新了权限经销商'"+authUserMessage+"'条数据");
       return view;

    }

}
